﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBExperment
{
    public partial class Borrow : UserControl
    {
        public DataTable BookList;
        private List<Book> books = new List<Book>();
        public DataTable TempList;
        public Borrow()
        {
            InitializeComponent();
            FillBookTable();
        }

        public void FillBookTable()
        {

            BookList = new DataTable();
            BookList.Columns.Add("编号");
            BookList.Columns.Add("书名");
            BookList.Columns.Add("状态");
            BookList.Columns.Add("学校");
            BookList.Columns.Add("图书馆");
            BookList.Columns.Add("类别");


            books = Data.FindBookList();

            object[] values = new object[6];

            for (int i = 0; i < books.Count; i++)
            {
                if (books[i].Status ==0)
                {
                    values[0] = books[i].ID;
                    values[1] = books[i].BName;
                    values[2] = books[i].Status;
                    values[3] = books[i].School;
                    values[4] = books[i].Liberty;
                    values[5] = books[i].Type;
                    BookList.Rows.Add(values);
                }
            }

            TempList = new DataTable();
            TempList.Columns.Add("编号");
            TempList.Columns.Add("书名");
            TempList.Columns.Add("状态");
            TempList.Columns.Add("学校");
            TempList.Columns.Add("图书馆");
            TempList.Columns.Add("类别");


            DGV_Borrow.DataSource = BookList;
            DGV_Borrow.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DGV_Borrow.Columns[0].FillWeight = 20;
            DGV_Borrow.Columns[1].FillWeight = 20;
            DGV_Borrow.Columns[2].FillWeight = 20;
            DGV_Borrow.Columns[3].FillWeight = 20;
            DGV_Borrow.Columns[4].FillWeight = 20;
            DGV_Borrow.Columns[5].FillWeight = 20;

            DGV_TempList.DataSource = TempList;
            DGV_TempList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DGV_TempList.Columns[0].FillWeight = 20;
            DGV_TempList.Columns[1].FillWeight = 20;
            DGV_TempList.Columns[2].FillWeight = 20;
            DGV_TempList.Columns[3].FillWeight = 20;
            DGV_TempList.Columns[4].FillWeight = 20;
            DGV_TempList.Columns[5].FillWeight = 20;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddBookForm addBookForm = new AddBookForm();
            addBookForm.ShowDialog();
            FillBookTable();
        }

        private void btn_Find_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < BookList.Rows.Count; i++)
            {
                BookList.Rows[i][0] = false;
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {

        }

        private void DGV_Book_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void DGV_Borrow_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            object[] values = new object[6];

            for (int i = 0; i < 6; i++)
            {
                values[i] = BookList.Rows[DGV_Borrow.CurrentCell.RowIndex][i];
            }
            TempList.Rows.Add(values);
            BookList.Rows.Remove(BookList.Rows[DGV_Borrow.CurrentCell.RowIndex]);

        }


        private void DGV_TempList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            object[] values = new object[6];
            for (int i = 0; i < 6; i++)
            {
                values[i] = TempList.Rows[DGV_TempList.CurrentCell.RowIndex][i];
            }
            BookList.Rows.Add(values);
            TempList.Rows.Remove(TempList.Rows[DGV_TempList.CurrentCell.RowIndex]);
            
        }

        public void WriteRecord(int tid,int bookid)
        {
            string date = DateTime.Now.ToString();
            DateTime d = DateTime.Parse(date);
            string sd = d.ToString("yyyy-MM-dd");

            string sql = @"INSERT INTO tb_LibraryCard (TID,BookID,[Date]) VALUES (" + tid + "," + bookid + ",'" + sd + "')";
            //MessageBox.Show(sql);
            DBExperment.Data.AddData(sql);
            sql = "update tb_booklist set status = 1 where bookid = " + bookid;
            DBExperment.Data.AddData(sql);
        }

        public void ClearRecord()
        {
            TempList.Clear();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            if (TempList.Rows.Count < 11)
            {
                int tid=123213;
                for (int i = 0; i < TempList.Rows.Count; i++)
                {
                    WriteRecord(tid,Convert.ToInt32(TempList.Rows[i][0]));
                }
                MessageBox.Show("提交成功!");
                ClearRecord();
            }
            else
            {
                MessageBox.Show("一次借书不能超过10本!");

            }
        }
    }

}
